package Logic;

import java.util.Vector;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import System.SystemLogger;

public class User extends Person {
	//statics
	private static Encryptor encryptor = new Encryptor();;
	private static int globalID = 0;
	private final static String[] fields = {"username","password","fname","lname","email","sex","bdate","joindate","lastlogin"};
	//class
	private int id;
	private String firstName;
	private String lastName;
	private String emailAddress;
	private String gender;
	private String birthDate;
	private String joinDate;
	private String lastLoginDate;		
	private Vector<Rank> ranks = new Vector<Rank>(1);
	private double averageRank = 0;
	
	
	
	/*-------------------------------------------------- 
	 ----------------   constructors   -----------------
	 ---------------------------------------------------*/
	
	public User(String[] vals) throws Exception{
		try{	
			setValues(vals);
		}
		catch(Exception ex){
			SystemLogger.getInstance().send("Exception: " + ex.getMessage());
			throw ex;
		}		
	}
	
	public User(Node node) throws Exception{
		int attNum = fields.length;
		NamedNodeMap attributes;
		String[] vals = new String[attNum];					
		attributes = node.getAttributes();
		for (int j=0; j<attNum; j++)
			vals[j] = attributes.getNamedItem(fields[j]).getNodeValue();
		
		try{	
			setValues(vals);
		}
		catch(Exception ex){
			throw ex;
		}		
	}
	
	
	public void setValues(String[] vals) throws Exception{
		if (vals.length != fields.length)
			throw new Exception("User : number of values incorrect");
		id = globalID;
		globalID++;
		username = vals[0].toLowerCase();
		password = vals[1];
		firstName = vals[2];
		lastName = vals[3];
		emailAddress = vals[4];
		gender = vals[5];
		birthDate = vals[6];
		joinDate = vals[7];
		lastLoginDate = vals[8];
	}
	
	public void addRank(Rank rank){
		int ranksNum = ranks.size();
		int i = 0;
		while (i<ranksNum){
			if (rank.getMovie().getID() <= ranks.get(i).getMovie().getID())
				break;
			i++;
		}
		ranks.add(i,rank);		
		double ranksMul = averageRank * ranksNum;
		ranksMul += rank.getRank();
		averageRank = ranksMul / (ranksNum + 1);		
	}
	
	public void printRanks(){
		int size = ranks.size();
		String movie;
		int rank;
		System.out.println(username + " : " + averageRank);
		for (int i=0; i<size; i++){
			movie = ranks.get(i).getMovie().toString();
			rank = ranks.get(i).getRank(); 
			System.out.println(movie + " ranked " + rank);
		}
		System.out.println("----------------------------------------");
	}
	
	public boolean isCorrectPassword(String password){
		return encryptor.MD5Password(password).equals(this.password);
	}
	
	public static String encrypt(String pass){
		return encryptor.MD5Password(pass);
	}
	
	public boolean isSaw(Movie m){
		int size = ranks.size();
		for (int i=0; i<size; i++)
			if (ranks.get(i).getMovie().equals(m))
				return true;
		return false;
		
	}
	
	public String toString(){
		return username;
	}
	
	public int getID() {
		return id;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(String birthDate) {
		this.birthDate = birthDate;
	}

	public String getUsername() {
		return username;
	}

	public String getLastName() {
		return lastName;
	}

	public String getJoinDate() {
		return joinDate;
	}

	public String getLastLoginDate() {
		return lastLoginDate;
	}

	public double getAverageRank() {
		return averageRank;
	}

	public void setAverageRank(double averageRank) {
		this.averageRank = averageRank;
	}

	public Vector<Rank> getRanks() {
		return ranks;
	}

	public void setRanks(Vector<Rank> ranks) {
		this.ranks = ranks;
	}

	public String[] getVals() {
		String [] ans = {this.username,this.password,this.firstName,this.lastName,this.emailAddress,
						this.gender,this.birthDate,this.joinDate,this.lastLoginDate};
		return ans;
	}

	public String[] getFields() {
		return this.fields;
	}

}
